﻿using Dapper;
using OMG.Fill.Potato.Model.Ent;
using ProjectShare;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace OMG.Fill.Potato.Services
{
    /// <summary>
    /// 马铃薯技术推广机构及人员情况统计表通用操作类
    /// </summary>
    public class PotPromotions
    {
        /// <summary>
        /// 添加
        /// </summary>
        public virtual ResResult<int> AddInfo(PotPromotionEntity model) {
            ResResult<int> obj = new ResResult<int>();
            string strSql = @"INSERT INTO dbo.T_PotPromotion";
            strSql +="(";
            strSql +="Id,";
            strSql +="ReportId,";
            strSql +="InstituNum,";
            strSql +="InstituNum_City,";
            strSql +="InstituNum_Area,";
            strSql +="InstituNum_town,";
            strSql +="PeopleNum,";
            strSql +="PeopleNum_County,";
            strSql +="PeopleNum_City,";
            strSql +="PeopleNum_man,";
            strSql +="PeopleNum_woman,";
            strSql +="PeopleNum_age1,";
            strSql +="PeopleNum_age2,";
            strSql +="PeopleNum_age3,";
            strSql +="PeopleNum_age4,";
            strSql +="PeopleNum_xlss,";
            strSql +="PeopleNum_xlbk,";
            strSql +="PeopleNum_xldz,";
            strSql +="PeopleNum_xlzz,";
            strSql +="PeopleNum_xlqt,";
            strSql +="PeopleNum_zczgj,";
            strSql +="PeopleNum_zcfgj,";
            strSql +="PeopleNum_zczj,";
            strSql +="PeopleNum_zccj,";
            strSql +="PeopleNum_zcqt)";
            strSql +=" values(";
            strSql +="@Id,";
            strSql +="@ReportId,";
            strSql +="@InstituNum,";
            strSql +="@InstituNum_City,";
            strSql +="@InstituNum_Area,";
            strSql +="@InstituNum_town,";
            strSql +="@PeopleNum,";
            strSql +="@PeopleNum_County,";
            strSql +="@PeopleNum_City,";
            strSql +="@PeopleNum_man,";
            strSql +="@PeopleNum_woman,";
            strSql +="@PeopleNum_age1,";
            strSql +="@PeopleNum_age2,";
            strSql +="@PeopleNum_age3,";
            strSql +="@PeopleNum_age4,";
            strSql +="@PeopleNum_xlss,";
            strSql +="@PeopleNum_xlbk,";
            strSql +="@PeopleNum_xldz,";
            strSql +="@PeopleNum_xlzz,";
            strSql +="@PeopleNum_xlqt,";
            strSql +="@PeopleNum_zczgj,";
            strSql +="@PeopleNum_zcfgj,";
            strSql +="@PeopleNum_zczj,";
            strSql +="@PeopleNum_zccj,";
            strSql +="@PeopleNum_zcqt)";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    model.Id= CustomFun.GetGuid();
                    int result = dbCon.Execute(strSql, model);
                    if (result > 0)
                    {
                        obj.model = result;
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "添加成功！";
                    }
                    else
                    {
                        obj.model = result;
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "添加失败！数量为:" + result;
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("新增马铃薯技术推广机构及人员情况统计表对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        public virtual ResResult<int> UpdateInfo(PotPromotionEntity model)
        {
            ResResult<int> obj = new ResResult<int>();
            string strSql = @"UPDATE dbo.T_PotPromotion set ";
            strSql+="ReportId = @ReportId,";
            strSql+="InstituNum = @InstituNum,";
            strSql+="InstituNum_City = @InstituNum_City,";
            strSql+="InstituNum_Area = @InstituNum_Area,";
            strSql+="InstituNum_town = @InstituNum_town,";
            strSql+="PeopleNum = @PeopleNum,";
            strSql+="PeopleNum_County = @PeopleNum_County,";
            strSql+="PeopleNum_City = @PeopleNum_City,";
            strSql+="PeopleNum_man = @PeopleNum_man,";
            strSql+="PeopleNum_woman = @PeopleNum_woman,";
            strSql+="PeopleNum_age1 = @PeopleNum_age1,";
            strSql+="PeopleNum_age2 = @PeopleNum_age2,";
            strSql+="PeopleNum_age3 = @PeopleNum_age3,";
            strSql+="PeopleNum_age4 = @PeopleNum_age4,";
            strSql+="PeopleNum_xlss = @PeopleNum_xlss,";
            strSql+="PeopleNum_xlbk = @PeopleNum_xlbk,";
            strSql+="PeopleNum_xldz = @PeopleNum_xldz,";
            strSql+="PeopleNum_xlzz = @PeopleNum_xlzz,";
            strSql+="PeopleNum_xlqt = @PeopleNum_xlqt,";
            strSql+="PeopleNum_zczgj = @PeopleNum_zczgj,";
            strSql+="PeopleNum_zcfgj = @PeopleNum_zcfgj,";
            strSql+="PeopleNum_zczj = @PeopleNum_zczj,";
            strSql+="PeopleNum_zccj = @PeopleNum_zccj,";
            strSql+="PeopleNum_zcqt = @PeopleNum_zcqt";
            strSql+= " where ReportId = @ReportId";
           try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    int result = dbCon.Execute(strSql, model);
                    if (result > 0)
                    {
                        string strRep = "update dbo.T_Report set Status='2',UpdateTime=@UpdateTime where Id=@ReportId";
                        dbCon.Execute(strRep, new { ReportId = model.ReportId, UpdateTime = DateTime.Now });
                        obj.model = result;
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "更新成功！";
                    }
                    else
                    {
                        obj.model = result;
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "更新失败！数量为:" + result;
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("更新马铃薯技术推广机构及人员情况统计表对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        public virtual ResResult<int> DeleteInfo(string id)
        {
            ResResult<int> obj = new ResResult<int>();
            string strSql = @"delete from dbo.T_PotPromotion where Id = @Id";
            int result;
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    result = dbCon.Execute(strSql, new { Id = id });
                    if (result > 0)
                    {
                        obj.model = result;
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "删除成功！";
                    }
                    else
                    {
                        obj.model = result;
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "删除失败！数量为:" + result;
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("删除马铃薯技术推广机构及人员情况统计表对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        public virtual ResResult<int> DeleteBatch(string[] ids)
        {
            ResResult<int> obj = new ResResult<int>();
            string strIds = string.Join(",", ids.Select(x => x.ToString()).ToArray());
            string strSql = @"delete from dbo.T_PotPromotion where Id in @ids";
            var dynamicSqlParam = new DynamicParameters();
            dynamicSqlParam.Add("ids", ids.ToArray(), null, null, null);
            int result;
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    result = dbCon.Execute(strSql, dynamicSqlParam);
                    if (result > 0)
                    {
                        obj.model = result;
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "删除成功！";
                    }
                    else
                    {
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "删除失败！数量为:" + result;
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                LogHelper.ErrorLog("获取数据字典表批量删除马铃薯技术推广机构及人员情况统计表表失败", ex);
                obj.message = ex.ToString().Trim();
                obj.status = DefaultKey.ErrorResult;
                return obj;
            }
        }
        /// <summary>
        /// 获取指定对象
        /// </summary>
        public ResResult<PotPromotionEntity> GetModel(string ReportId) {
            ReportServices reportService = new ReportServices();
            ResResult<PotPromotionEntity> obj = new ResResult<PotPromotionEntity>();
            string strSql = @"select * from dbo.T_PotPromotion where ReportId = @ReportId";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    if (reportService.CheckRepCount(ReportId, "T_PotPromotion"))
                    {
                        PotPromotionEntity entity = new PotPromotionEntity();
                        entity.ReportId = ReportId;
                        AddInfo(entity);
                    }
                    obj.model = dbCon.Query<PotPromotionEntity>(strSql, new { ReportId = ReportId }).FirstOrDefault();
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "获取成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("获取马铃薯技术推广机构及人员情况统计表对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 获取全部列表
        /// </summary>
        /// <returns></returns>
        public virtual ResResult<List<PotPromotionEntity>> GetList()
        {
            ResResult<List<PotPromotionEntity>> obj = new ResResult<List<PotPromotionEntity>>();
            string strSql = @"select * from dbo.T_PotPromotion";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    obj.model = dbCon.Query<PotPromotionEntity>(strSql).ToList();
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "获取成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("更新马铃薯技术推广机构及人员情况统计表对象信息失败", ex);
                return obj;
            }
        }
    }
}